package com.yy.controller.service.Impl;

import com.yy.context.BaseContext;
import com.yy.controller.mapper.ShoppingCartMapper;
import com.yy.controller.service.ShoppingCartService;
import com.yy.entity.ShoppingCart;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 获取购物车列表
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(userId);
        return shoppingCartList;
    }

    /**
     * 添加到购物车
     *
     * @param shoppingCart
     */
    @Override
    public void add(ShoppingCart shoppingCart) {
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        shoppingCart.setCreateTime(LocalDateTime.now());
        //是菜品
        if (shoppingCart.getDishId() != null) {
            //查询购物车是否已存在该菜品
            ShoppingCart sC = shoppingCartMapper.getByDishId(shoppingCart.getDishId(), userId);
            //不存在该菜品
            if (sC == null) {
                shoppingCartMapper.add(shoppingCart);
            } else {
                //已存在该菜品
                shoppingCartMapper.updateNumber(sC.getId(), shoppingCart.getPrice());
            }
        }
        //是套餐
        if (shoppingCart.getSetmealId() != null) {
            //查询购物车是否已存在该套餐
            ShoppingCart sC = shoppingCartMapper.getBySetmealId(shoppingCart.getSetmealId(), userId);
            if (sC == null) {
                //不存在
                shoppingCartMapper.add(shoppingCart);
            } else {
                //已存在
                shoppingCartMapper.updateNumber(sC.getId(), shoppingCart.getPrice());
            }
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void clear() {
        long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clear(userId);
    }

    /**
     * 根据id减少菜品或套餐数量数量
     *
     * @param id
     */
    @Override
    public void reduceById(Long id) {
        //先查看该条数据number是否为1
        ShoppingCart shoppingCart = shoppingCartMapper.getById(id);
        //为1
        if (shoppingCart.getNumber() == 1) {
            //删除该数据
            shoppingCartMapper.deleteById(id);
            return;
        }
        //不为1
        shoppingCartMapper.reduceById(id);
    }

    /**
     * 减少菜品或套餐数量通过菜品id(无口味菜品)
     *
     * @param dishId
     */
    @Override
    public void reduceByDsihId(Long dishId) {
        Long userId = BaseContext.getCurrentId();
        //先查看number是否为1
        ShoppingCart shoppingCart = shoppingCartMapper.getByDishId(dishId, userId);
        //为1
        if (shoppingCart.getNumber() == 1) {
            shoppingCartMapper.deleteById(shoppingCart.getId());
            return;
        }
        //不为1
        shoppingCartMapper.reduceByDishId(dishId);
    }

    /**
     * 减少菜品或套餐数量通过套餐id
     *
     * @param setmealId
     */
    @Override
    public void reduceBysetmealId(Long setmealId) {
        Long userId = BaseContext.getCurrentId();
        //先查看该条数据number是否为1
        ShoppingCart shoppingCart = shoppingCartMapper.getBySetmealId(setmealId, userId);
        //为1
        if (shoppingCart.getNumber() == 1) {
            shoppingCartMapper.deleteById(shoppingCart.getId());
            return;
        }
        //不为1
        shoppingCartMapper.reduceBysetmealId(setmealId);
    }

    /**
     * 增加菜品或套餐数量通过id
     *
     * @param id
     */
    @Override
    public void addById(Long id) {
        shoppingCartMapper.addById(id);
    }

}
